package com.tools.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.stream.Stream;


@Slf4j
public class SendSms {
    private static final SmsClient client;

    static {
        Credential cred = new Credential("AKIDve8cfTZgHbbMtEo9I3NUjX9WdV7ThGzb", "UmmW0C7zfAkLgm9JUaPAvSFl2A24sRnt");
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        client = new SmsClient(cred, "", clientProfile);
    }

    public static void sendCode(String code, String... phones) {
        try {
            SendSmsResponse resp = client.SendSms(buildReq(phones, code));
            log.info(SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }

    public static void sendNoticeTip(String... phones) {
        try {
            SendSmsResponse resp = client.SendSms(buildNoticesReq(phones));
            log.info(SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }

    private static SendSmsRequest buildNoticesReq(String[] phones) {
        SendSmsRequest req = new SendSmsRequest();

        phones = Stream.of(phones).map(a -> "86" + a).toArray(String[]::new);

        req.setPhoneNumberSet(phones);
//
//        String[] params = {code};
//        req.setTemplateParamSet(params);
        req.setTemplateID("807013");
        req.setSmsSdkAppid("1400461083");
        req.setSign("点滴记忆");
        return req;
    }

    private static SendSmsRequest buildReq(String[] phones, String code) {
        SendSmsRequest req = new SendSmsRequest();

        phones = Stream.of(phones).map(a -> "86" + a).toArray(String[]::new);

        req.setPhoneNumberSet(phones);

        String[] params = {code};
        req.setTemplateParamSet(params);

        req.setTemplateID("806008");
        req.setSmsSdkAppid("1400461083");
        req.setSign("点滴记忆");
        return req;
    }


    public static void main(String[] args) {
        sendCode("12111", "18821708656");
    }

}